package city.spring.modules.medicine.controller;

import city.spring.modules.medicine.entity.MedicineshelfEntity;
import city.spring.modules.medicine.entity.MedicineshelflevelEntity;
import city.spring.modules.medicine.entity.MedicinestorehouseEntity;
import city.spring.modules.medicine.service.MedicineshelfService;
import city.spring.modules.medicine.service.MedicineshelflevelService;
import city.spring.modules.medicine.service.MedicinestorehouseService;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;

/**
 * Controller：仓库货架管理
 *
 * @author 谢金婷
 * @date 2020/7/15 0026 14:20
 */
@RestController
@RequestMapping("/medicinestorehouseshelfManagement")
public class MedicinestorehouseShelfManagementController extends ApiController {
    /**
     * Service：仓库信息
     */
    private final MedicinestorehouseService storehouseService;
    /**
     * Service：货架信息
     */
    private final MedicineshelfService shelfService;
    /**
     * Service：货架层次信息
     */
    private final MedicineshelflevelService shelflevelService;

    public MedicinestorehouseShelfManagementController(MedicinestorehouseService storehouseService, MedicineshelfService shelfService, MedicineshelflevelService shelflevelService) {
        this.storehouseService = storehouseService;
        this.shelfService = shelfService;
        this.shelflevelService = shelflevelService;
    }


    @GetMapping("tree")
    public Object tree() {
        List<MedicinestorehouseEntity> storehouseEntities = storehouseService.list();
        ListMultimap<String, MedicineshelfEntity> shelfMultimap = getShelfMultimap(false);
        for (MedicinestorehouseEntity entity : storehouseEntities) {
            List<MedicineshelfEntity> entities = shelfMultimap.get(String.valueOf(entity.getId()));
            // 重新对列表进行排序
            entities.sort(Comparator.comparing(MedicineshelfEntity::getGmtCreate, Date::compareTo));
            entity.setChildren(entities);
        }
        return success(storehouseEntities);
    }

    @GetMapping(value = "tree", params = {"loadShellevel=true"})
    public Object treeLoadShellevel() {
        List<MedicinestorehouseEntity> storehouseEntities = storehouseService.list();
        ListMultimap<String, MedicineshelfEntity> shelfMultimap = getShelfMultimap(true);
        storehouseEntities.forEach(entity -> {
            List<MedicineshelfEntity> entities = shelfMultimap.get(String.valueOf(entity.getId()));
            // 重新对列表进行排序
            entities.sort(Comparator.comparing(MedicineshelfEntity::getGmtCreate, Date::compareTo));
            entity.setChildren(entities);
        });
        return success(storehouseEntities);
    }

    /**
     * 以 仓库ID 为键，收集每一个仓库的货架列表信息
     *
     * @return 每个仓库的货架列表信息
     */
    private ListMultimap<String, MedicineshelfEntity> getShelfMultimap(boolean loadShellevel) {
        // 获取所有货架列表
        List<MedicineshelfEntity> shelfEntities = shelfService.list();
        // 判断是否需要加载货架层次信息
        ListMultimap<String, MedicineshelflevelEntity> shelflevelMultimap = null;
        if (loadShellevel) {
            shelflevelMultimap = getShelflevelMultimap();
        }

        BiConsumer<ListMultimap<String, MedicineshelflevelEntity>, MedicineshelfEntity> loadChildren = (multimap, entity) -> {
            List<MedicineshelflevelEntity> entities = multimap.get(entity.getId());
            // 重新对列表进行排序
            entities.sort(Comparator.comparing(MedicineshelflevelEntity::getGmtCreate, Date::compareTo));
            entity.setChildren(entities);
        };

        // 以 仓库ID 为键，收集每一个货架的货架层次列表信息
        ListMultimap<String, MedicineshelfEntity> multimap = ArrayListMultimap.create();

        for (MedicineshelfEntity entity : shelfEntities) {
            if (loadShellevel) {
                loadChildren.accept(shelflevelMultimap, entity);
            }
            multimap.put(String.valueOf(entity.getStorehouseId()), entity);
        }
        return multimap;
    }

    /**
     * 以 货架ID 为键，收集每一个货架的货架层次列表信息
     *
     * @return 每个房间的床位列表信息
     */
    private ListMultimap<String, MedicineshelflevelEntity> getShelflevelMultimap() {
        // 获取所有房间列表
        List<MedicineshelflevelEntity> shelfLevelEntities = shelflevelService.list();
        // 以 楼层ID 为键，收集每一个楼层的房间列表信息
        ListMultimap<String, MedicineshelflevelEntity> multimap = ArrayListMultimap.create();

        shelfLevelEntities.forEach(entity -> multimap.put(entity.getShelfId(), entity));

        return multimap;
    }
}
